<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 找车位（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>停车场有一横排车位&#xff0c;0代表没有停车&#xff0c;1代表有车。至少停了一辆车在车位上&#xff0c;也至少有一个空位没有停车。</p> 
<p>为了防剐蹭&#xff0c;需为停车人找到一个车位&#xff0c;使得距<span style="color:#fe2c24;">停车人的车</span>最近的车辆的距离是最大的&#xff0c;返回此时的<strong>最大距离</strong>。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<ol><li>一个用半角逗号分割的停车标识字符串&#xff0c;停车标识为0或1&#xff0c;0为空位&#xff0c;1为已停车。</li><li>停车位最多100个。</li></ol> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出一个整数记录最大距离。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td>输入</td><td>1,0,0,0,0,1,0,0,1,0,1</td></tr><tr><td>输出</td><td>2</td></tr><tr><td>说明</td><td> <p>当车停在第3个位置上时&#xff0c;离其最近的的车距离为2&#xff08;1到3&#xff09;。</p> <p>当车停在第4个位置上时&#xff0c;离其最近的的车距离为2&#xff08;4到6&#xff09;。</p> <p>其他位置距离为1。</p> <p>因此最大距离为2</p> </td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题类似于<a href="https://blog.csdn.net/qfc_128220/article/details/128584271" title="华为OD机试 - 新员工座位&#xff08;Java &amp; JS &amp; Python&#xff09;_伏城之外的博客-CSDN博客">华为OD机试 - 新员工座位&#xff08;Java &amp; JS &amp; Python&#xff09;_伏城之外的博客-CSDN博客</a></p> 
<p></p> 
<p>本题也可以采用两步走&#xff1a;</p> 
<ul><li>正向遍历一遍&#xff0c;确认每个空闲车位的左边的最大停车距离&#xff08;即当前空闲车位的左边连续空闲车位个数&#43;1&#xff09;</li><li>反向遍历一遍&#xff0c;确认每个空闲车位的右边的最大停车距离&#xff08;即当前空闲车位的右边连续空闲车位个数&#43;1&#xff09;</li></ul> 
<p>而每个空闲车位的最大停车距离&#xff0c;取其左、右最大停车距离中的较小值。</p> 
<p>最后&#xff0c;返回所有空闲车位中的最大停车距离即可。</p> 
<p></p> 
<p>本题需要注意的是&#xff0c;首尾如果是空闲车位的话&#xff1a;</p> 
<ul><li>对第0个空闲车位&#xff0c;其最大停车距离&#xff0c;取得是其右边的停车距离</li><li>对第n-1个空闲车位&#xff0c;其最大停车距离&#xff0c;取得是其左边的停车距离</li></ul> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    int[] nums &#61; Arrays.stream(sc.nextLine().split(&#34;,&#34;)).mapToInt(Integer::parseInt).toArray();
    System.out.println(getResult(nums));
  }

  public static int getResult(int[] nums) {
    int n &#61; nums.length;

    // leftFree[i] 表示第i个车位的左边空闲车位个数
    int[] leftFree &#61; new int[n];
    for (int i &#61; 1; i &lt; n; i&#43;&#43;) {
      if (nums[i] &#61;&#61; 1) continue;

      if (nums[i - 1] &#61;&#61; 0) {
        leftFree[i] &#61; leftFree[i - 1] &#43; 1;
      } else {
        leftFree[i] &#61; 0;
      }
    }

    // rightFree[i] 表示第i个车位的右边空闲车位个数
    int[] rightFree &#61; new int[n];
    for (int i &#61; n - 2; i &gt;&#61; 0; i--) {
      if (nums[i] &#61;&#61; 1) continue;

      if (nums[i &#43; 1] &#61;&#61; 0) {
        rightFree[i] &#61; rightFree[i &#43; 1] &#43; 1;
      } else {
        rightFree[i] &#61; 0;
      }
    }

    // 如果第0个车位是空闲车位&#xff0c;那么第0个车位的最大距离就是它右边的空闲车位个数
    // 如果第n-1个车位是空闲车位&#xff0c;那么第n-1个车位的最大安全距离就是它左边的空闲车位个数
    int ans &#61; Math.max(rightFree[0], leftFree[n - 1]);

    // 对于第i个车位&#xff08;i取值1~n-2&#xff09;&#xff0c;其最大距离取min(leftFree[i], rightFree[i])
    for (int i &#61; 1; i &lt; n - 1; i&#43;&#43;) {
      if (nums[i] &#61;&#61; 1) continue;
      ans &#61; Math.max(ans, Math.min(leftFree[i], rightFree[i]));
    }

    // ans记录的空闲车位数&#xff0c;转化为距离要&#43;1
    return ans &#43; 1;
  }
}
</code></pre> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const nums &#61; line.split(&#34;,&#34;).map(Number);
  console.log(getResult(nums));
});

function getResult(nums) {
  const n &#61; nums.length;

  // leftFree[i] 表示第i个车位的左边空闲车位个数
  const leftFree &#61; new Array(n).fill(0);
  for (let i &#61; 0; i &lt; n; i&#43;&#43;) {
    if (nums[i] &#61;&#61; 1) continue;

    if (nums[i - 1] &#61;&#61; 0) {
      leftFree[i] &#61; leftFree[i - 1] &#43; 1;
    } else {
      leftFree[i] &#61; 0;
    }
  }

  // rightFree[i] 表示第i个车位的右边空闲车位个数
  const rightFree &#61; new Array(n).fill(0);
  for (let i &#61; n - 2; i &gt;&#61; 0; i--) {
    if (nums[i] &#61;&#61; 1) continue;

    if (nums[i &#43; 1] &#61;&#61; 0) {
      rightFree[i] &#61; rightFree[i &#43; 1] &#43; 1;
    } else {
      rightFree[i] &#61; 0;
    }
  }

  // 如果第0个车位是空闲车位&#xff0c;那么第0个车位的最大距离就是它右边的空闲车位个数
  // 如果第n-1个车位是空闲车位&#xff0c;那么第n-1个车位的最大安全距离就是它左边的空闲车位个数
  let ans &#61; Math.max(rightFree[0], leftFree[n - 1]);

  // 对于第i个车位&#xff08;i取值1~n-2&#xff09;&#xff0c;其最大距离取min(leftFree[i], rightFree[i])
  for (let i &#61; 1; i &lt; n - 1; i&#43;&#43;) {
    if (nums[i] &#61;&#61; 1) continue;
    ans &#61; Math.max(ans, Math.min(leftFree[i], rightFree[i]));
  }

  return ans &#43; 1;
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
nums &#61; list(map(int, input().split(&#34;,&#34;)))


# 算法入口
def getResult():
    n &#61; len(nums)

    # leftFree[i] 表示第i个车位的左边空闲车位个数
    leftFree &#61; [0]*n
    for i in range(n):
        if nums[i] &#61;&#61; 1:
            continue

        if nums[i-1] &#61;&#61; 0:
            leftFree[i] &#61; leftFree[i-1] &#43; 1
        else:
            leftFree[i] &#61; 0

    # rightFree[i] 表示第i个车位的右边空闲车位个数
    rightFree &#61; [0]*n
    for i in range(n-2, -1, -1):
        if nums[i] &#61;&#61; 1:
            continue

        if nums[i&#43;1] &#61;&#61; 0:
            rightFree[i] &#61; rightFree[i&#43;1] &#43; 1
        else:
            rightFree[i] &#61; 0

    # 如果第0个车位是空闲车位&#xff0c;那么第0个车位的最大距离就是它右边的空闲车位个数
    # 如果第n-1个车位是空闲车位&#xff0c;那么第n-1个车位的最大安全距离就是它左边的空闲车位个数
    ans &#61; max(rightFree[0], leftFree[n-1])

    # 对于第i个车位&#xff08;i取值1~n-2&#xff09;&#xff0c;其最大距离取min(leftFree[i], rightFree[i])
    for i in range(1, n-1):
        if nums[i] &#61;&#61; 1:
            continue

        ans &#61; max(ans, min(leftFree[i], rightFree[i]))

    # ans记录的空闲车位数&#xff0c;转化为距离要&#43;1
    return ans &#43; 1


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define MAX(a, b) ((a) &gt; (b) ? (a) : (b))
#define MIN(a, b) ((a) &lt; (b) ? (a) : (b))

#define MAX_SIZE 100

int main() {
    int nums[MAX_SIZE];
    int nums_size &#61; 0;

    while (scanf(&#34;%d&#34;, &amp;nums[nums_size&#43;&#43;])) {
        if (getchar() !&#61; &#39;,&#39;) break;
    }

    // leftFree[i] 表示第i个车位的左边空闲车位个数
    int* leftFree &#61; (int*) calloc(nums_size, sizeof(int));

    for (int i &#61; 1; i &lt; nums_size; i&#43;&#43;) {
        if (nums[i] &#61;&#61; 1) continue;

        if (nums[i - 1] &#61;&#61; 0) {
            leftFree[i] &#61; leftFree[i - 1] &#43; 1;
        } else {
            leftFree[i] &#61; 0;
        }
    }

    // rightFree[i] 表示第i个车位的右边空闲车位个数
    int* rightFree &#61; (int*) calloc(nums_size, sizeof(int));

    for (int i &#61; nums_size - 2; i &gt;&#61; 0; i--) {
        if (nums[i] &#61;&#61; 1) continue;

        if (nums[i &#43; 1] &#61;&#61; 0) {
            rightFree[i] &#61; rightFree[i &#43; 1] &#43; 1;
        } else {
            rightFree[i] &#61; 0;
        }
    }

    // 如果第0个车位是空闲车位&#xff0c;那么第0个车位的最大距离就是它右边的空闲车位个数
    // 如果第n-1个车位是空闲车位&#xff0c;那么第n-1个车位的最大安全距离就是它左边的空闲车位个数
    int ans &#61; MAX(rightFree[0], leftFree[nums_size - 1]);

    // 对于第i个车位&#xff08;i取值1~n-2&#xff09;&#xff0c;其最大距离取min(leftFree[i], rightFree[i])
    for (int i &#61; 1; i &lt; nums_size - 1; i&#43;&#43;) {
        if (nums[i] &#61;&#61; 1) continue;
        ans &#61; MAX(ans, MIN(leftFree[i], rightFree[i]));
    }

    // ans记录的空闲车位数&#xff0c;转化为距离要&#43;1
    printf(&#34;%d\n&#34;, ans &#43; 1);

    return 0;
}</code></pre> 
<p> </p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>